package com.rd.p2p.app.additional.experience;


import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.executer.ExecuterHelper;
import com.qdlc.p2p.biz.executer.impl.ExperienceExecuter;
import com.qdlc.p2p.biz.service.BorrowService;
import com.qdlc.p2p.biz.service.ExperienceService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.Borrow;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.ExperienceModel;

/**
 * App体验券
 * 
 * @version 2.0
 * @author ylx
 * @date 2015年5月19日 上午11:44:55
 */
public class AppUserExperienceAction extends BaseAction<ExperienceModel> implements ModelDriven<ExperienceModel> {
	
	@Resource
	private ExperienceService experienceService;
	
	@Resource
	private BorrowService borrowService;
	
	private User user;
	
	private Map<String, Object> data ;
	
	@Action(value="/app/user/experienceList")
	public void list() throws Exception {
		user = getSessionUser();
		model.setUserId(user.getUserId());
		String status = paramString("status");
		if(StringUtil.isBlank(status)){
			model.setStatus((byte)99);
		}
		PageDataList<ExperienceModel> pagaDataList = experienceService.findByModel(model);
		int total = pagaDataList.getPage().getTotal();
		data = new HashMap<String, Object>();
		data.put("total", total);
		data.put("rows", pagaDataList);
		data.put("status", model.getStatus());
		data.put("resCode", "1");
		printJson(getStringOfJpaObj(data));
	}
	
	@Action(value="/app/experience/useable", interceptorRefs = {@InterceptorRef("session"), @InterceptorRef("globalStack") })
	public void useable() throws Exception {
		user = getSessionUser();
		String uuid = paramString("uuid");
		Borrow borrow = borrowService.find(uuid);
		ExperienceExecuter experienceExecuter = ExecuterHelper.getExecuter(ExperienceExecuter.class);
		data = new HashMap<String, Object>();
		data.put("experienceAmount", experienceExecuter.findCanTenderExperienceAmount(borrow, user.getUserId()));
		data.put("experiences", experienceExecuter.findUsableExperience(user.getUserId()));
		printJson(getStringOfJpaObj(data));
	}
	
	@Action(value="/app/experience/initUseable", interceptorRefs = {@InterceptorRef("session"), @InterceptorRef("globalStack") })
	public void initUseable() throws Exception {
		user = getSessionUser();
		String uuid = paramString("uuid");
		Borrow borrow = borrowService.find(uuid);
		ExperienceExecuter experienceExecuter =  ExecuterHelper.getExecuter(ExperienceExecuter.class);
		data = new HashMap<String, Object>();
		data.put("experienceAmount", experienceExecuter.findCanTenderExperienceAmount(borrow, user.getUserId()));
		data.put("experienceCount", experienceExecuter.countUsableExperience(user.getUserId()));
		printJson(getStringOfJpaObj(data));
	}
	
}
